package com.huangkailong.gitimgbed.core;

import cn.hutool.core.util.StrUtil;
import cn.hutool.system.SystemUtil;
import com.google.inject.Module;
import com.google.inject.Singleton;
import com.huangkailong.gitimgbed.service.ImgBedService;
import com.huangkailong.gitimgbed.service.JgitService;
import com.huangkailong.gitimgbed.service.impl.ImgBedServiceImpl;
import com.huangkailong.gitimgbed.service.impl.JgitServiceImpl;
import com.huangkailong.gitimgbed.util.ToastUtil;
import com.huangkailong.gitimgbed.view.MainView;
import com.huangkailong.gitimgbed.view.SettingView;
import de.saxsys.mvvmfx.guice.MvvmfxGuiceApplication;
import javafx.stage.Stage;

import java.util.List;

/**
 * 基于 GIT 图床工具
 *
 * @author huangkl
 * @since 1.0.0
 */
public class GitImgBedApplication extends MvvmfxGuiceApplication {
    public static void main(String[] args) {
        launch(args);
    }

    private ImgBedConfiguration imgBedConfiguration;

    @Override
    public void startMvvmfx(Stage stage) throws Exception {
        ViewDispatcher.addStage(MainView.class, stage);
        stage.setTitle("Git img bed");
        stage.setScene(ViewDispatcher.getScene(MainView.class));
        stage.setOnShown(event -> {
            final String gitRepositoryUrl = this.imgBedConfiguration.getGitRepositoryUrl();
            if(StrUtil.isBlank(gitRepositoryUrl)){
                ViewDispatcher.showView(SettingView.class);
                ToastUtil.toast("请配置信息!");
            }
        });
        stage.show();
        stage.setOnCloseRequest(event -> {
            stage.close();
            System.exit(0);
        });
    }

    @Override
    public void initGuiceModules(List<Module> modules) throws Exception {
        modules.add(binder->{
            String configPath = SystemUtil.getUserInfo().getHomeDir()+ "/.imgBed/imgBed.properties";
            this.imgBedConfiguration = new ImgBedConfiguration(configPath);
            binder.bind(ImgBedConfiguration.class).toInstance(imgBedConfiguration);
            binder.bind(JgitService.class).to(JgitServiceImpl.class).in(Singleton.class);
            binder.bind(ImgBedService.class).to(ImgBedServiceImpl.class).in(Singleton.class);
        });
    }
}
